package com.app.micaihu.view.main.game

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.game.GameContent
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.configure.UmEventId
import com.app.micaihu.configure.UmEventId.list
import com.app.micaihu.utils.MobclickAgentUtils
import com.app.micaihu.view.main.game.adapter.GameClassListAdapter
import com.app.micaihu.view.main.game.contract.GameClassListContract
import com.app.micaihu.view.main.game.presenter.GameClassListPresenter
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import kotlinx.android.synthetic.main.main_arms_list.*
import kotlinx.android.synthetic.main.view_common_title_rv.*
import kotlinx.android.synthetic.main.view_common_title_rv.refreshLayout

/**
 * 军团官职当前列表
 */
class GameClassListActivity : BaseMvpActivity<GameClassListContract.View, GameClassListPresenter>(), GameClassListContract.View {

    override fun getLayoutId() = R.layout.view_common_title_rv

    override fun getBundle(bundle: Bundle) {
        type = bundle.getInt(Config.Extra.type, 0)
        title = bundle.getString(Config.Extra.title, "")
    }

    override fun getPresenter() = GameClassListPresenter()

    private var type = 0
    private var title = ""

    private val gameClassListAdapter: GameClassListAdapter by lazy {
        val gameClassListAdapter = GameClassListAdapter()
        rvList.layoutManager = WrapContentLinearLayoutManager(thisContext)
        gameClassListAdapter
    }

    override fun setData() {
        mCommonTitle.title = title
        refreshLayout.setEnableLoadMore(true)
        refreshLayout.setEnableAutoLoadMore(true)

        rvList.adapter = gameClassListAdapter
        mPresenter?.gameClassList(type)
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            mPresenter?.refresh()
        }
        refreshLayout.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
        val listener: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit = { adapter, view, position ->
            val item = gameClassListAdapter.getItem(position);
            val intent = Intent()
            intent.putExtra(Config.Extra.PARAMETER1, item.articleId)
            intent.setClass(thisActivity, GameDetailActivity::class.java)
            startActivity(intent)
        }
        gameClassListAdapter.setOnItemClickListener(listener)
    }


    override fun handleGameClassList(result: BaseBean<List<GameContent>?>?, loadType: Int) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        if (result == null) {
            if (gameClassListAdapter.data.isNotEmpty()) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    refreshLayout.finishLoadMore(false)
                }
            } else {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    gameClassListAdapter.setEmptyView(errorView)
                } else {
                    gameClassListAdapter.setEmptyView(getEmptyView())
                }
            }
            gameClassListAdapter.setEmptyView(errorView)
        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        gameClassListAdapter.setList(it)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        gameClassListAdapter.addData(it)
                    }
                    if (it.isNullOrEmpty()) {
                        gameClassListAdapter.setEmptyView(getEmptyView())
                        refreshLayout.finishLoadMoreWithNoMoreData()
                    } else {
                        refreshLayout.finishLoadMore(Config.delayLoadMore)
                    }
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }
    }

}